#include<iostream>
#include<atomic>
#include<chrono>


#include "ThreadPool.h"

using namespace std;


atomic<int> task_counter{ 0 };

void Task(int id) {
	this_thread::sleep_for(chrono::milliseconds(100));
	cout << "Task " << id << " executed by thread " << this_thread::get_id() << endl;
	++task_counter;
}

void Producer(Pool::ThreadPool& pool, int produce_id, int num_tasks) {
	for (int i = 0; i < num_tasks; ++i) {
		int task_id = produce_id * 1000 + i;
		post_task_to_thread_pool_(pool, Task, task_id);
		//pool.Post(Task, task_id);
		cout << "Producer " << produce_id << " posted task " << task_id << endl;
	}
}

int main(void) {


	const int num_producers = 4;
	const int num_tasks_per_producer = 10;
	const int num_threads_in_pool = 2;

	auto threadPool = create_thread_pool_(num_threads_in_pool);
	//Pool::ThreadPool threadPool(num_threads_in_pool);

	vector<thread> producers;

	for (int i = 0; i < num_producers; ++i) {
		producers.emplace_back(Producer, ref(*threadPool), i, num_tasks_per_producer);
	}

	for (auto& work : producers) {
		work.join();
	}

	while (task_counter < num_producers * num_tasks_per_producer) {
		this_thread::sleep_for(chrono::milliseconds(100));
	}

	cout << "All tasks completed. Total tasks executed: " << task_counter << endl;
	shutdown_thread_pool_(*threadPool);

	return 0;
}
